package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class AddressBook here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AddressBook
{
    // instance variables - replace the example below with your own
    private ArrayList<Person> persons;

    /**
     * Constructor for objects of class AddressBook
     */
    public AddressBook()
    {
        // initialise instance variables
        persons = new ArrayList<Person>();
    }

    /**
     * Creates a person object with the given attributes, and stores it in the address book
     */
    public void addPerson(int id, String name,String address, String zipCode, String city, String phoneNumber)
    {
        Person p = new Person(id, name, address, zipCode, city, phoneNumber);
        persons.add(p);
    }
    
    /**
     * Returns the person with the given ID
     */
    public Person getPerson(int id)
    {
        Person result = null;
        for(Person person : persons)
        {
            if(person.getId() == id)
            {
                result = person;
            }
        }
        
        return result;
    }
    
    /**
     * Removes the person with the given ID
     */
    public void removePerson(int id)
    {
        //standard search-pattern:
        boolean found = false;
        int index = 0;
        while(found == false && index < persons.size())
        {
            //find the person at index-position in the arraylist "persons"
            Person person = persons.get(index);
            //find that persons ID
            int personID = person.getId();
            
            //if that ID is equal to the parameter- id
            if(personID == id)
            {
                //then we have found the person we looked for (at the arraylist-index "index")
                found = true;
            }
            else
            {
                //else we increase the index by 1 and try again :)
                index = index + 1;
            }
        }//end loop
        
        //if we found what we sought (after loop has ended)
        if(found == true)
        {
            //then the index is pointing to the person, who we want deleted!
            persons.remove(index);
        }
    }
    
    /**
     * returns the address book
     */
    public ArrayList<Person> getAllPersons()
    {
        return persons;
    }
}
